package com.ebelter.bodyfatscale.util;

import android.content.Context;
import android.content.SharedPreferences;

//这个类只保存用户信息，其他一律不保存
public class UserSpUtil {
    public static final String APP_NAME = "UserSp";
    public static Context mContext;

    public static void init(Context context){
        mContext = context;
    }

    public static String readString(String key) {
        return getSharedPreferences().getString(key, "");
    }

    public static String readString(String key, String value) {
        return getSharedPreferences().getString(key, value);
    }

    public static void writeString(String key, String value) {
        getSharedPreferences().edit().putString(key, value).apply();
    }

    public static boolean readBoolean(String key) {
        return getSharedPreferences().getBoolean(key, false);
    }

    public static boolean readBoolean(String key, boolean defBool) {
        return getSharedPreferences().getBoolean(key, defBool);
    }

    public static void writeBoolean(String key, boolean value) {
        getSharedPreferences().edit().putBoolean(key, value).apply();
    }

    public static int readInt(String key) {
        return getSharedPreferences().getInt(key, 0);
    }

    public static int readInt(String key, int value) {
        return getSharedPreferences().getInt(key, value);
    }

    public static void writeInt(String key, int value) {
        getSharedPreferences().edit().putInt(key, value).apply();
    }

    public static long readLong(String key) {
        return getSharedPreferences().getLong(key, 0);
    }
    public static long readLong(String key,long def) {
        return getSharedPreferences().getLong(key, def);
    }

    public static float readFloat(String key) {
        return getSharedPreferences().getFloat(key, 0);
    }

    public static void writeLong(String key, long value) {
        getSharedPreferences().edit().putLong(key, value).apply();
    }
    public static void writeFloat(String key, float value) {
        getSharedPreferences().edit().putFloat(key, value).apply();
    }

    public static void remove(String key) {
        getSharedPreferences().edit().remove(key).apply();
    }

    public static void removeAll() {
        getSharedPreferences().edit().clear().apply();
    }

    private static SharedPreferences getSharedPreferences() {
        return mContext.getSharedPreferences(APP_NAME, Context.MODE_PRIVATE);
    }
}
